package backend.user.manager;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Projections;

import core.address.model.BasicAddress;
import core.address.model.city.City;
import core.address.model.continent.Continent;
import core.address.model.country.Country;
import core.address.model.region.Region;
import core.address.model.state.State;
import core.profile.model.BasicCredential;
import core.profile.model.BasicProfile;
import core.role.model.BasicRoles;
import core.util.HibernateCoreUtil;

public class UserManager extends HibernateCoreUtil
{
	public List<BasicProfile> getUserList(int start,int end)
	{
		Session session = HibernateCoreUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<BasicProfile> users =session.createQuery("from BasicProfile").setFirstResult(start).setMaxResults(10).list();
		session.getTransaction().commit();
		return users ; 
	}
	public Integer getCount()
	{
		Session session = HibernateCoreUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Integer count = (Integer) session.createCriteria(BasicProfile.class).setProjection(Projections.rowCount()).uniqueResult();
		session.getTransaction().commit();
		return count ; 
	}
	public BasicRoles getRoleByUuid(Long uuid)
	{
		Session session = HibernateCoreUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		BasicRoles br =  (BasicRoles) session.get(BasicRoles.class, uuid);	
		session.getTransaction().commit();
		return br ;
	}
	public List<BasicProfile> getAdminUser()
	{
		Session session = HibernateCoreUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<BasicProfile> users =session.createQuery("from BasicProfile where usertype=4").list();
		session.getTransaction().commit();
		return users ; 
	}
	
	public BasicProfile getUserUserId(String userid)
	{
		Session session = HibernateCoreUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		BasicProfile bp=null;
		Iterator<BasicProfile> users =(Iterator)session.createQuery("from BasicProfile where emailid='"+userid+"'").iterate();
		while(users.hasNext())
		{
			bp = users.next() ;
		}
		session.getTransaction().commit();
		return bp; 
	}
	
	
	
	public List<BasicRoles> getRoles()
	{
		
		Session session = HibernateCoreUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<BasicRoles> roles =session.createQuery("from BasicRoles").list();
		
		session.getTransaction().commit();
		return roles ; 
	}
	
	
	
	public BasicProfile getUserByUuid(Long uuid)
	{
		Session session = HibernateCoreUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		BasicProfile bp = (BasicProfile) session.get(BasicProfile.class, uuid);	
		session.getTransaction().commit();
		return bp ;
	}
	public BasicCredential getcredetialByprofileid(Long uuid)
	{
		BasicCredential bc=null;
		Session session = HibernateCoreUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<BasicCredential> bclst =session.createQuery("from BasicCredential where basicprofile_uuid='"+uuid+"' ").list();
		Iterator<BasicCredential> bcitr = null ;
		if(bclst!=null)
		{
			bcitr=bclst.iterator();
		while(bcitr.hasNext())
		{
			bc=bcitr.next();
		}
		}
		session.getTransaction().commit();
		return bc ;
	}
	public BasicAddress getCurrentAddressByUuid(Long uuid)
	{
		Session session = HibernateCoreUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		BasicAddress ba = (BasicAddress) session.get(BasicAddress.class, uuid);	
		session.getTransaction().commit();
		return ba ;
	}
	public BasicAddress getPermanentAddressByUuid(Long uuid)
	{
		Session session = HibernateCoreUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		BasicAddress ba = (BasicAddress) session.get(BasicAddress.class, uuid);	
		session.getTransaction().commit();
		return ba ;
	}
	
	public City getCityByName(String city)
	{
		City cty=null;
		Session session = HibernateCoreUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Iterator<City> cityitr =session.createQuery("from City where cityname='"+city+"' ").iterate();
		if(cityitr!=null)
		{
		while(cityitr.hasNext())
		{
			cty=cityitr.next();
		}
		}
		session.getTransaction().commit();
		return cty ;
	}
	public BasicProfile updateUser(int usertype, Boolean status, String title, String firstname, String lastname, String emailId, String gender, String companyname,Long uuid)
	{
		Session session = HibernateCoreUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		BasicProfile bp=(BasicProfile)session.get(BasicProfile.class,uuid);
		bp.setCompanyname(companyname);
		bp.setEmailId(emailId);
		bp.setFirstname(firstname);
		bp.setGender(gender);
		bp.setLastname(lastname);
		bp.setStatus(status);
		bp.setTitle(title);
		bp.setUsertype(usertype);
		session.update(bp);
		session.getTransaction().commit();
		return bp;
	}
	public void saveOrUpdate(BasicProfile bp)
	{
		Session session = HibernateCoreUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		session.saveOrUpdate(bp);
		session.getTransaction().commit();
	}
	public BasicCredential updateCredential(String password, String confirmPassword,Long uuid)
	{
		Session session = HibernateCoreUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		BasicCredential bc=(BasicCredential)session.get(BasicCredential.class,uuid);
		bc.setPassword(password);
		bc.setPasswordreminder(confirmPassword);
		session.saveOrUpdate(bc);
		session.getTransaction().commit();
		return bc;
	}
	
	public BasicAddress updateCurrentAddress(String addressLine1,String addressLine2,String phone,String phonebusiness,String mobile,String postalcode,City city,Country country,State state,Continent continent,Region region,Long uuid)
	{
		Transaction transaction = null;
		Session session = HibernateCoreUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		BasicAddress ba=(BasicAddress)session.get(BasicAddress.class,uuid);
		ba.setAddressLine1(addressLine1);
		ba.setAddressLine2(addressLine2);
		ba.setPhone(phone);
		ba.setPhonebusiness(phonebusiness);
		ba.setMobile(mobile);
		ba.setPostalcode(postalcode);
		ba.setCity(city);
		ba.setCountry(country);
		ba.setState(state);
		ba.setContinent(continent);
		ba.setRegion(region);
		try
		{
			transaction = session.beginTransaction();
			ba = (BasicAddress)session.merge(ba);
			transaction.commit();
		}
		catch(HibernateException he)
		{
			he.printStackTrace();
		}
		finally
		{
		
		}
		return ba;
	}
	public BasicAddress updatePermanentAddress(String addressLine1,String addressLine2,String phone,String phonebusiness,String mobile,String postalcode,City city,Country country,State state,Continent continent,Region region,Long uuid)
	{
		Transaction transaction;
		Session session = HibernateCoreUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		BasicAddress ba=(BasicAddress)session.get(BasicAddress.class,uuid);
		ba.setAddressLine1(addressLine1);
		ba.setAddressLine2(addressLine2);
		ba.setPhone(phone);
		ba.setPhonebusiness(phonebusiness);
		ba.setMobile(mobile);
		ba.setPostalcode(postalcode);
		ba.setCity(city);
		ba.setCountry(country);
		ba.setState(state);
		ba.setContinent(continent);
		ba.setRegion(region);
		try
		{
			transaction = session.beginTransaction();
			ba = (BasicAddress)session.merge(ba);
			transaction.commit();
		}
		catch(HibernateException he)
		{
			he.printStackTrace();
		}
		finally
		{
		
		}
		return ba;
	}

	public BasicProfile updateAdminUser(int usertype, String firstname,
			String lastname, String emailId, Long uuid) 
	{
		// TODO Auto-generated method stub
		Session session = HibernateCoreUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		BasicProfile bp=(BasicProfile)session.get(BasicProfile.class,uuid);
		bp.setEmailId(emailId);
		bp.setFirstname(firstname);
		bp.setLastname(lastname);
		bp.setUsertype(usertype);
		session.update(bp);
		session.getTransaction().commit();
		return bp;
	}
	
}
